var initcatelink = webrootpath+"cate/initcatelink";
var findtreecatedatalink = webrootpath+"cate/findtreecatedatalink";
var inserttreenodelink = webrootpath+"cate/inserttreenodelink";
var selectcateid = "";
$(function () {
    getTreeSQDData(0);
    index_shenqingdan_btncate();
});

/***
 * 初始化分类树
 */
function index_shenqingdan_btncate() {
    $("#index_shenqingdan_btncate").click(function () {
        var r =  window.confirm("您确定初始化整体分类吗？");
        if(r){
            $.get(initcatelink,function (data) {
                alert(data.msg);
                initTree();
            });
        }
    });
}

/***
 * 获取申请单分类数据
 */
function getTreeSQDData(_node) {
    $.get(findtreecatedatalink,{node:_node},function (data) {
        if (data.code=='1'){
            initTree(data.data);
        }
    });
}

/***
 * 初始化树结构
 */
function initTree(_treedata) {
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        callback:{
            onRightClick:function (event, treeId, treeNode) {
                zTreeObj.selectNode(treeNode);
                var clientx = event.clientX-210;
                var clienty = event.clientY-50;
                $("#index_shenqingdan_youjian").css({"margin-left":clientx,"margin-top":clienty,"display":"block"});
                selectcateid = treeNode.cateid;
            },
            onExpand:function (event, treeId, treeNode) {
                console.log(treeNode);
                var zAsync = treeNode.zAsync;
                //展开
                var treenodeid = treeNode.cateid;
                $.get(findtreecatedatalink,{node:treenodeid},function (data) {
                    if (data.code=='1'){
                        var sonnode = data.data.datalist;
                        zTreeObj.addNodes(treeNode, sonnode);


                    }
                });
            },
            onCollapse:function (event, treeId, treeNode) {
                //折叠后
                var treenodeid = treeNode.cateid;
                zTreeObj.removeChildNodes(treeNode);
                getTreeSQDData(treeNode.parentid);


            }
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    zTreeObj = $.fn.zTree.init($("#index_shenqingdan_tree"), setting, _treedata.datalist);
}

/***
 * 添加节点弹出窗口
 */
function index_shenqingdan_youjian_addnode() {
    index_shenqingdan_unb();
    $("#index_shenqingdan_treenode_name").val("");
    $("#index_shenqingdan_treenode_desc").val("");
    $("#index_shenqingdan_treenode").modal('show');
}

/***
 * 提交更改
 */
function index_shenqingdan_treenode_submit() {
    var index_shenqingdan_treenode_name = $.trim($("#index_shenqingdan_treenode_name").val());
    var index_shenqingdan_treenode_desc = $.trim($("#index_shenqingdan_treenode_desc").val());

    if (selectcateid=="" || index_shenqingdan_treenode_name=="" || index_shenqingdan_treenode_desc==""){
        return false;
    }
    $.post(inserttreenodelink,{index_shenqingdan_treenode_name:index_shenqingdan_treenode_name,index_shenqingdan_treenode_desc:index_shenqingdan_treenode_desc,selectcateid:selectcateid},function (data) {
        if (data.code=="1"){
            $("#index_shenqingdan_treenode").modal('hide');
            alert(data.data);
            getTreeSQDData(data.data);

        }
    });
}

/***
 * 取消右键菜单
 */
function index_shenqingdan_unb() {
    $("#index_shenqingdan_youjian").css("display","none");
}
function index_shenqingdan_over() {
    $("#index_shenqingdan_youjian").css("display","block");
}